package com.keson.door2.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import com.arcsoft.face.ErrorInfo;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FaceFeature;
import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.util.ImageUtils;
import com.keson.door2.bean.Face;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Cache {
    public static ArrayList<Face> faceFeatureList = new ArrayList<>();
    public static String photoPath = Environment.getExternalStorageDirectory()+"/DCIM/Camera";
    public static String featurePath = Environment.getExternalStorageDirectory()+"/Feature";
    public static boolean canMoveCard = true;
    public static boolean hasIdCard = false;
    public static HashMap<String, String> IDCardInfo= new HashMap<>();

    public static void initLocalFaceFeatureFromPhoto(FaceEngine faceEngine){
        //    考勤人员文件夹目录
        File[] localFiles =new File(photoPath).listFiles();
        for (File photo:localFiles) {
            if(photo.isDirectory()){
                continue;
            }
            Bitmap bitmap = BitmapFactory.decodeFile(photo.getAbsolutePath());
            bitmap = ImageUtils.alignBitmapForBgr24(bitmap);
            byte[] bgr24 = ImageUtils.bitmapToBgr24(bitmap);
            List<FaceInfo> faceInfoList = new ArrayList<>();
            int detectCode = faceEngine.detectFaces(bgr24, bitmap.getWidth(), bitmap.getHeight(), FaceEngine.CP_PAF_BGR24, faceInfoList);
            if (detectCode == ErrorInfo.MOK&&!faceInfoList.isEmpty()) {
                FaceFeature faceFeature = new FaceFeature();
                detectCode = faceEngine.extractFaceFeature(bgr24, bitmap.getWidth(), bitmap.getHeight(), FaceEngine.CP_PAF_BGR24, faceInfoList.get(0), faceFeature);
                if (detectCode == ErrorInfo.MOK){
                    Face face = new Face();
                    face.setFaceFeature(faceFeature);
                    face.setId(photo.getName());
                    faceFeatureList.add(face);
//                    writeTofile(faceFeature);
                }
            }else {
                Log.i("初始化本地人脸特征表", "文件："+photo.getName()+"，未检测到人脸，错误代码："+detectCode);
            }
        }
        Log.i("初始化本地人脸特征表", "个数为："+faceFeatureList.size());
    }

    public static void initLocalFaceFeatureFromFile(){
        faceFeatureList.clear();
        for (File file:new File(featurePath).listFiles()) {
            try {
                byte[] b = InputStream2ByteArray(file.getAbsolutePath());
                Face face = new Face();
                face.setFaceFeature(new FaceFeature(b));
                face.setId(file.getName().split(",")[0]);
                face.setName(file.getName().split(",")[1]);
                faceFeatureList.add(face);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static byte[] InputStream2ByteArray(String filePath) throws IOException {

        InputStream in = new FileInputStream(filePath);
        byte[] data = toByteArray(in);
        in.close();
        return data;
    }

    private static byte[] toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    }

    public static void writeTofile(String fileName, FaceFeature faceFeature) {
        File file = new File(featurePath + "/" + fileName);
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(faceFeature.getFeatureData());

        } catch (IOException ex) {
        } finally {
            try {
                if(os!=null){
                    os.close();
                }
            } catch (IOException ex1) {
            }
        }
    }
}
